package org.yu.team2023.flashdays;

import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.EntityAttributeModificationEvent;

import java.util.function.Consumer;
import java.util.function.Supplier;

public class BSEntityUtil {
    public BSEntityUtil() {
    }

    public static void safeAttribute(LivingEntity living, Attribute attr, Consumer<Double> attributeConsumer) {
        if (living.getAttributes().hasAttribute(attr)) {
            attributeConsumer.accept(living.getAttributeValue(attr));
        }

    }

    public static void safeAttribute(LivingEntity living, Supplier<Attribute> supplier, Consumer<Double> attributeConsumer) {
        Attribute attr = supplier.get();
        if (attr != null) {
            if (living.getAttributes().hasAttribute(attr)) {
                attributeConsumer.accept(living.getAttributeValue(attr));
            }

        }
    }

    public static void runIfLivingDamage(DamageSource source, Consumer<LivingEntity> consumer) {
        LivingEntity living = null;
        Entity var5 = source.getEntity();
        if (var5 instanceof LivingEntity p) {
            living = p;
        } else {
            var5 = source.getDirectEntity();
            if (var5 instanceof LivingEntity p) {
                living = p;
            }
        }

        if (living != null) {
            consumer.accept(living);
        }

    }

    public static void runIfPlayerDamage(DamageSource source, Consumer<Player> consumer) {
        Player player = null;
        Entity var5 = source.getEntity();
        if (var5 instanceof Player p) {
            player = p;
        } else {
            var5 = source.getDirectEntity();
            if (var5 instanceof Player p) {
                player = p;
            }
        }

        if (player != null) {
            consumer.accept(player);
        }

    }

    public static <T extends LivingEntity> void addIfNotExists(EntityAttributeModificationEvent event, EntityType<T> type, Attribute attribute) {
        if (!event.has(type, attribute)) {
            event.add(type, attribute);
        }

    }
}